//
//  YWEquipmentTroubleRecord.m
//  maintain
//
//  Created by 郑想 on 15/11/11.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWEquipmentTroubleRecord.h"
#import "MJExtension.h"
#import "YWCommonTool.h"
#import "YWMission.h"
#import "YWHandler.h"
#import "YWReportTroubleModel.h"
#import "YWEquipment.h"
#import "YWEquipmentType.h"

@implementation YWEquipmentTroubleRecord
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{@"equipmentFieldRecord" : @"equipmentFields",
             @"recordPictures" : @"pictures"};
}
+ (NSDictionary*)mj_objectClassInArray
{
    return @{ @"recordPictures" : @"YWPicture",
              @"equipmentFieldRecord" : @"YWEquipmentFieldRecord",
              @"replacements" : @"YWTroubleRecordReplacements"};
}

- (void)mj_keyValuesDidFinishConvertingToObject
{
    if (self.replacements.count) {
        __block NSString *replacementName = @"";
        [self.replacements enumerateObjectsUsingBlock:^(YWTroubleRecordReplacements*  _Nonnull replaceN, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx>0) {
                replacementName = [replacementName stringByAppendingString:@","];
            }
            replacementName = [replacementName stringByAppendingString:replaceN.replacementName];
        }];
        self.replacementName = replacementName;
    }
    if (self.handler) {
        self.handlerName = self.handler.userName;
    }
    if (self.equipment) {
        self.equipmentId = self.equipment.equipmentId;
    }
    if (self.equipmentType) {
        self.equipmentTypeId = self.equipmentType.equipmentTypeId;
    }
    if (self.trouble) {
        self.troubleId = self.trouble.troubleId;
    }
}

@end

@implementation YWTroubleRecordReplacements

@end

@implementation YWTroubleJson

+ (NSDictionary*)mj_objectClassInArray
{
    return @{ @"pictures" : @"YWPicture"};
}
@end

@implementation YWEquipmentTroubleRecordJson
+ (NSArray *)mj_ignoredPropertyNames
{
    return @[@"clientCurrentTime",@"clientExecuteTime",@"clientScanTime",@"replacementName",@"scanTime"];
}

+ (NSDictionary*)mj_objectClassInArray
{
    return @{ @"replacements" : @"YWTroubleRecordReplacements",
              @"equipmentFields" : @"YWEquipmentFieldRecord",
              @"pictures" : @"YWPicture"};
}

- (void)mj_keyValuesDidFinishConvertingToObject
{
    if (self.equipment) {
        self.equipmentId = self.equipment.equipmentId;
    }
    if (self.trouble) {
        self.reportTime = self.trouble.reportTime;
        self.troubleId = self.trouble.troubleId;
    }
    if (self.createTime) {
        NSString *dayMonthString = [YWCommonTool showTime:self.createTime / 1000 andFormatter:KTimeFormatWithOutDayMonth];
        self.dayMonthString = dayMonthString;
    }else{
        self.dayMonthString = @"";
    }
    
    if (self.trouble.troubleContact && self.reporter==nil) {
        YWHandler *handler = [[YWHandler alloc] init];
        handler.userName = self.trouble.troubleContact.contactName;
        handler.mobile = self.trouble.troubleContact.contactNo;
        self.reporter = handler;
    }
    if (self.replacements.count) {
        __block NSString *replacementName = @"";
        [self.replacements enumerateObjectsUsingBlock:^(YWTroubleRecordReplacements*  _Nonnull replaceN, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx>0) {
                replacementName = [replacementName stringByAppendingString:@","];
            }
            replacementName = [replacementName stringByAppendingString:replaceN.replacementName];
        }];
        self.replacementName = replacementName;
    }
}

@end
